package com.ruoyi.web.cp.service.merge;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.web.core.config.ConfigLoader;
import com.ruoyi.web.cp.model.Game;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.web.cp.service.merge.ZbConstant.*;

@Slf4j
@Service
public class MergeAqfbService {
    @Autowired
    private ConfigLoader configLoader;

    public void mergeAqfb() {
        log.info("======> 合并aqfb开始");
        long beginTime = System.currentTimeMillis();

        String rmGamePath = configLoader.getRmPath() + "aq-gameScore.txt";
        String fbGamePath = configLoader.getRmPath() + "fb-gameScore.txt";

        List<Game> rmGames = Game.getGames(rmGamePath, "");
        List<Game> obGames = Game.getGames(fbGamePath, "aqfb");

        Game.calc(rmGames, obGames, null, "rm");
        writeMergeResult(rmGames, configLoader.getRmPath(), "aqfb-merge");
        log.info(StrUtil.format("======> 合并rmfb结束,耗时:{}", (System.currentTimeMillis() - beginTime)));
    }

    private void writeMergeResult(List<Game> games, String path, String name) {
        List<String> allGames = new ArrayList<>();
        List<String> fitGames = new ArrayList<>();
        List<String> fitGames30 = new ArrayList<>();
        List<String> fitGames150 = new ArrayList<>();
        List<String> fitGames100 = new ArrayList<>();
        List<String> noMatch = new ArrayList<>(); //未匹配

        for (Game game : games) {
            try {
                StringBuilder sb = new StringBuilder();
                sb.append(game.getDateStr()).append("\t");
                sb.append(game.getLeague()).append("\t");
                sb.append(game.getTeamA()).append("\t");
                sb.append(game.getTeamB()).append("\t");
                sb.append(game.getGameId());
                if (game.getMemo() != null) {
                    sb.append("\t").append(game.getMemo());
                    noMatch.add(sb.toString());
                }
                allGames.add(sb.toString());
                List<String> lists = new ArrayList<>();
                List<String> lists30 = new ArrayList<>();
                List<String> lists150 = new ArrayList<>();
                List<String> lists100 = new ArrayList<>();
                for (Game.Score score : game.getScores()) {
                    StringBuilder sb1 = new StringBuilder();
                    sb1.append(score.getScore()).append("\t");
                    sb1.append(score.getPlv()).append("\t");
                    sb1.append(score.getPlv2()).append("\t");
                    if (score.getPlv3() != null) {
                        sb1.append(score.getPlv3()).append("\t");
                    }
                    if (score.getPlv4() != null) {
                        sb1.append(score.getPlv4()).append("\t");
                    }
                    String fit = score.getFit();
                    String plv2 = score.getPlv2();
                    String str = sb1.toString();

                    //记录利润>=0
                    if (fit != null && Double.valueOf(fit) > 0) {
                        String s = str + "收：" + fit;
                        lists.add(s.replace("\t", "|"));
                    }
                    sb1.append(fit);
                    allGames.add(str + fit);

                    //记录利润>=30
                    if (fit != null && Double.valueOf(fit) >= MIN_FIT) {
                        String s = str + "收：" + fit;
                        lists30.add(s.replace("\t", "|"));
                    }
                    //记录正波>=150
                    if (plv2 != null && Double.valueOf(plv2) >= MAX_ZB_PLV_150) {
                        String s = str + "收：" + fit;
                        lists150.add(s.replace("\t", "|"));
                    }
                    //记录正波>=100
                    if (plv2 != null && Double.valueOf(plv2) >= MAX_ZB_PLV_100) {
                        String s = str + "收：" + fit;
                        lists100.add(s.replace("\t", "|"));
                    }
                    if ("双进".equals(score.getScore())) {
                        String s = "FB双进:" + score.getPlv();
                        if (CollUtil.isNotEmpty(lists100)) {
                            lists100.add(s);
                        }
                        if (CollUtil.isNotEmpty(lists150)) {
                            lists150.add(s);
                        }
                    }
                    if ("大小".equals(score.getScore())) {
                        String s = "FB大小:" + score.getPlv();
                        if (CollUtil.isNotEmpty(lists100)) {
                            lists100.add(s);
                        }
                        if (CollUtil.isNotEmpty(lists150)) {
                            lists150.add(s);
                        }
                    }
                    if ("亚盘".equals(score.getScore())) {
                        String s = "FB亚盘:" + score.getPlv();
                        if (CollUtil.isNotEmpty(lists100)) {
                            lists100.add(s);
                        }
                        if (CollUtil.isNotEmpty(lists150)) {
                            lists150.add(s);
                        }
                    }
                }
                if (CollUtil.isNotEmpty(lists)) {
                    fitGames.add(sb.toString().replace("\t", ","));
                    fitGames.addAll(lists);
                }
                if (CollUtil.isNotEmpty(lists30)) {
                    fitGames30.add(sb.toString().replace("\t", ","));
                    fitGames30.addAll(lists30);
                }
                if (CollUtil.isNotEmpty(lists150)) {
                    fitGames150.add(sb.toString().replace("\t", ","));
                    fitGames150.addAll(lists150);
                }
                if (CollUtil.isNotEmpty(lists100)) {
                    fitGames100.add(sb.toString().replace("\t", ","));
                    fitGames100.addAll(lists100);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
        FileUtil.writeLines(allGames, path + name + ".txt", "utf-8");
        FileUtil.writeLines(fitGames, path + name + "2.txt", "utf-8");
        FileUtil.writeLines(fitGames30, path + name + "30.txt", "utf-8");
        FileUtil.writeLines(fitGames150, path + name + "150.txt", "utf-8");
        FileUtil.writeLines(fitGames100, path + name + "100.txt", "utf-8");
        log.warn("aqfb150+:{}", JSONObject.toJSONString(fitGames150));
        FileUtil.writeLines(noMatch, path + "merge" + System.getProperty("file.separator") + "aqfb-match" + ".txt", "utf-8");
    }
}
